//
//  ViewController.m
//  21-NetWork
//
//  Created by 殷殷明静 on 16/8/11.
//  Copyright © 2016年 殷明静. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
- (IBAction)synbtn:(id)sender;
- (IBAction)asynbtn:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *netImageView;
- (IBAction)urlSessionBtn:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)synbtn:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"GET";
    // 同步
    // 参数一:NSMutableURLRequest 可变的request
    // 参数二:
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    if (data) {
        _netImageView.image = [UIImage imageWithData:data];
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"点击");
}
- (IBAction)asynbtn:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"GET";
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        
        _netImageView.image = [UIImage imageWithData:data];
    }];
}
- (IBAction)urlSessionBtn:(id)sender {
    // 单例的地址不变化
   /** NSURLSession *session = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:@"http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg"];
    // 参数一 request
    // 参数二:block块
    // session的block处于异步线程,需要返回主线程刷新UI(图片赋值,tableView collectionView 的刷新)
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"GET";
   NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       // 返回主线程
       dispatch_async(dispatch_get_main_queue(), ^{
           // 主线程加载
           _netImageView.image = [UIImage imageWithData:data];
           
       });
        
    }];
    // 执行任务
    [task resume];*/
    
#pragma mark post 请求
    NSURLSession *urlSession = [NSURLSession sharedSession];
    NSURL *imageURL = [NSURL URLWithString:@"http://open3.bantangapp.com/base/app/init"];
    NSMutableURLRequest *imageURLRequest = [NSMutableURLRequest requestWithURL:imageURL];
    imageURLRequest.HTTPMethod = @"POST";
    // 请求体需要一个NSData ,字符串转data 
    imageURLRequest.HTTPBody = [@"app_id=com.jzyd.BanTang&app_installtime=1467276561&app_versions=5.8.5&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&device_token=1aa2741a8e58dbf700d96e588b551a5bf7f8a1e222eb9948c836ba717ce0d48e&os_versions=10.0&screensize=750&track_device_info=iPhone8%2C1&track_deviceid=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&v=17" dataUsingEncoding:NSUTF8StringEncoding];
    // URLSession, 网络请求
    // 参数1: request
    // 参数2: Block块, CallBack回调
    // 注意: Session的回调中处于异步线程, 需要返回主线程刷新UI(图片赋值, TableView CollectionView的刷新)
    NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:imageURLRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        // 主线程执行图片加载
        dispatch_async(dispatch_get_main_queue(), ^{
            //            _networkImage.image = [UIImage imageWithData:data];
            id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@", result);
        });
    }];
    // 让 dataTask 执行
    [dataTask resume];
}
@end
